package com.huawei.hms.framework.network.restclient.hwhttp;

import android.content.Context;
import com.huawei.hms.framework.common.CheckParamUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.HttpCache;
import com.huawei.hms.framework.network.restclient.hianalytics.DefaultRCEventListener.DefaultFactory;
import com.huawei.hms.framework.network.restclient.hianalytics.RCEventListener;
import com.huawei.hms.framework.network.restclient.hwhttp.Submit.Factory;
import com.huawei.hms.framework.network.restclient.hwhttp.cronet.CronetNegotiateManager;
import com.huawei.hms.framework.network.restclient.hwhttp.cronet.CronetRequestTaskFactory;
import com.huawei.hms.framework.network.restclient.hwhttp.cronet.QuicHint;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.RCDns;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DNKeeper;
import com.huawei.hms.framework.network.restclient.hwhttp.okhttp.OkRequestTaskFactory;
import com.huawei.hms.framework.network.restclient.hwhttp.plugin.PluginInterceptor;
import com.huawei.hms.framework.network.restclient.hwhttp.url.HttpUrl;
import com.huawei.hms.framework.network.restclient.hwhttp.urlconnection.URLConnectionRequestTaskFactory;
import com.huawei.hms.framework.network.util.ContextUtil;
import com.huawei.secure.android.common.ssl.SecureSSLSocketFactory;
import com.huawei.secure.android.common.ssl.SecureX509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import o.fuv;

public class HttpClient implements Factory {
    private static X509TrustManager DEFAULT_X509_TRUST_MANAGER = null;
    private static final int EMUI_INTERNAL_OKHTTP_VERSION_DATE = 181123;
    private static final String TAG = "HttpClient";
    private HttpCache cache;
    private final ClientConfiguration clientConfiguration;
    private RequestTask.Factory cronetFactory;
    private RequestTask.Factory defaultFactory;
    private final DNKeeper dnKeeper;
    private RCDns dns;
    private RCEventListener.Factory eventListenerFactory;
    private HostnameVerifier hostnameVerifier;
    private final List<Interceptor> interceptors;
    private boolean isReportable;
    private final List<Interceptor> networkInterceptors;
    private boolean quicEnabled;
    private SSLSocketFactory sslSocketFactory;
    private X509TrustManager trustManager;
    private boolean weakNetworkRetryEnable;

    public static final class Builder {
        private static final int MIN_CALL_TIME_OUT = 20;
        private HttpCache cache = null;
        private com.huawei.hms.framework.network.restclient.hwhttp.ClientConfiguration.Builder clientConfiguration = new com.huawei.hms.framework.network.restclient.hwhttp.ClientConfiguration.Builder();
        private DNKeeper dnKeeper;
        private RCEventListener.Factory eventListenerFactory;
        private HostnameVerifier hostnameVerifier;
        private final List<Interceptor> interceptors = new ArrayList();
        private boolean isReportable = false;
        private final List<Interceptor> networkInterceptors = new ArrayList();
        private boolean quicEnabled;
        private List<QuicHint> quicHints = new ArrayList();
        private SSLSocketFactory sslSocketFactory;
        private X509TrustManager trustManager;
        private boolean weakNetworkRetryEnable = true;

        public Builder addInterceptor(Interceptor interceptor) {
            if (interceptor == null) {
                throw new IllegalArgumentException("interceptor == null");
            }
            this.interceptors.add(interceptor);
            if (HttpClientGlobalInstance.getInstance().getHttpClient() != null) {
                HttpClientGlobalInstance.getInstance().getHttpClient().addInterceptor(interceptor);
            }
            return this;
        }

        public Builder addInterceptors(List<Interceptor> list) {
            if (list == null) {
                throw new IllegalArgumentException("interceptors == null");
            }
            if (HttpClientGlobalInstance.getInstance().getHttpClient() != null) {
                for (Interceptor access$1200 : list) {
                    HttpClientGlobalInstance.getInstance().getHttpClient().addInterceptor(access$1200);
                }
            }
            return this;
        }

        public Builder addNetInterceptors(List<Interceptor> list) {
            if (list == null) {
                throw new IllegalArgumentException("interceptors == null");
            }
            if (HttpClientGlobalInstance.getInstance().getHttpClient() != null) {
                for (Interceptor access$1300 : list) {
                    HttpClientGlobalInstance.getInstance().getHttpClient().addNetworkInterceptor(access$1300);
                }
            }
            return this;
        }

        public Builder addNetworkInterceptor(Interceptor interceptor) {
            if (interceptor == null) {
                throw new IllegalArgumentException("interceptor == null");
            }
            this.networkInterceptors.add(interceptor);
            if (HttpClientGlobalInstance.getInstance().getHttpClient() != null) {
                HttpClientGlobalInstance.getInstance().getHttpClient().addNetworkInterceptor(interceptor);
            }
            return this;
        }

        public Builder connectTimeout(int i) {
            this.clientConfiguration.connectTimeout(i);
            return this;
        }

        public Builder readTimeout(int i) {
            this.clientConfiguration.readTimeout(i);
            return this;
        }

        public Builder retryTimeOnConnectionFailure(int i) {
            this.clientConfiguration.retryTimeOnConnectionFailure(i);
            return this;
        }

        public Builder callTimeout(int i) {
            if (i < 20) {
                Logger.w(HttpClient.TAG, "When callTimeout less than 20, it sets to zero");
                i = 0;
            }
            this.clientConfiguration.callTimeout(i);
            return this;
        }

        public Builder pingInterval(int i) {
            if (i < 0) {
                i = 0;
                Logger.w(HttpClient.TAG, "When pingInterval less than zero, it sets to zero");
            }
            this.clientConfiguration.pingInterval(i);
            return this;
        }

        public Builder writeTimeout(int i) {
            this.clientConfiguration.writeTimeout(i);
            return this;
        }

        public Builder clientConfiguration(@Nonnull com.huawei.hms.framework.network.restclient.hwhttp.ClientConfiguration.Builder builder) {
            CheckParamUtils.checkNotNull(builder, "clientConfiguration == null");
            this.clientConfiguration = builder;
            return this;
        }

        @Deprecated
        public Builder dnKeeper(DNKeeper dNKeeper) {
            CheckParamUtils.checkNotNull(dNKeeper, "dnKeeper == null");
            this.dnKeeper = dNKeeper;
            return this;
        }

        public Builder sslSocketFactory(SSLSocketFactory sSLSocketFactory, X509TrustManager x509TrustManager) {
            CheckParamUtils.checkNotNull(sSLSocketFactory, "sslSocketFactory == null");
            CheckParamUtils.checkNotNull(x509TrustManager, "trustManager == null");
            this.sslSocketFactory = sSLSocketFactory;
            this.trustManager = x509TrustManager;
            return this;
        }

        public Builder hostnameVerifier(HostnameVerifier hostnameVerifier) {
            CheckParamUtils.checkNotNull(hostnameVerifier, "hostnameVerifier == null");
            this.hostnameVerifier = hostnameVerifier;
            return this;
        }

        public Builder eventListenerFactory(RCEventListener.Factory factory) {
            if (factory == null) {
                throw new NullPointerException("eventListenerFactory == null");
            }
            this.eventListenerFactory = factory;
            return this;
        }

        public Builder isReportable(boolean z) {
            this.isReportable = z;
            return this;
        }

        public Builder enableQuic(boolean z) {
            this.quicEnabled = z;
            return this;
        }

        public Builder weakNetworkRetryEnable(boolean z) {
            this.weakNetworkRetryEnable = z;
            return this;
        }

        public HttpClient build() {
            return new HttpClient();
        }
    }

    private HttpClient(Builder builder) {
        this.interceptors = new ArrayList();
        this.networkInterceptors = new ArrayList();
        this.cache = null;
        this.weakNetworkRetryEnable = true;
        this.trustManager = builder.trustManager;
        this.sslSocketFactory = builder.sslSocketFactory;
        this.eventListenerFactory = builder.eventListenerFactory;
        this.isReportable = builder.isReportable;
        if (this.eventListenerFactory == null) {
            this.eventListenerFactory = new DefaultFactory(this.isReportable);
        }
        this.quicEnabled = builder.quicEnabled;
        if (this.trustManager == null) {
            trustManagerAndFactory();
        }
        this.hostnameVerifier = builder.hostnameVerifier;
        if (this.hostnameVerifier == null) {
            this.hostnameVerifier = SecureSSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
        }
        this.dnKeeper = builder.dnKeeper;
        this.interceptors.addAll(builder.interceptors);
        this.networkInterceptors.addAll(builder.networkInterceptors);
        this.clientConfiguration = builder.clientConfiguration.build();
        if (this.dns == null) {
            this.dns = RCDns.DEFAULT;
            this.dns.setDnstime(this.clientConfiguration.getConnectTimeout());
        }
        if (this.quicEnabled) {
            if (CronetNegotiateManager.getInstance().isSupportCronet()) {
                CronetNegotiateManager.getInstance().lazyInitHmsQuicLoader();
                CronetNegotiateManager.getInstance().loadQuicConf();
            } else {
                Logger.i(TAG, "system don't support cronet, so diable quic!!!");
                this.quicEnabled = false;
            }
        }
        this.cache = builder.cache;
        this.weakNetworkRetryEnable = builder.weakNetworkRetryEnable;
        this.defaultFactory = createFactory();
    }

    private void trustManagerAndFactory() {
        Throwable e;
        try {
            if (DEFAULT_X509_TRUST_MANAGER == null) {
                DEFAULT_X509_TRUST_MANAGER = new SecureX509TrustManager(ContextUtil.getContext());
            }
            this.trustManager = DEFAULT_X509_TRUST_MANAGER;
            this.sslSocketFactory = SecureSSLSocketFactory.getInstance(ContextUtil.getContext());
        } catch (IOException e2) {
            e = e2;
            Logger.w(TAG, "catch exception when create sslSocketFactory", e);
        } catch (NoSuchAlgorithmException e3) {
            e = e3;
            Logger.w(TAG, "catch exception when create sslSocketFactory", e);
        } catch (CertificateException e4) {
            e = e4;
            Logger.w(TAG, "catch exception when create sslSocketFactory", e);
        } catch (KeyStoreException e5) {
            e = e5;
            Logger.w(TAG, "catch exception when create sslSocketFactory", e);
        } catch (KeyManagementException e6) {
            e = e6;
            Logger.w(TAG, "catch exception when create sslSocketFactory", e);
        } catch (IllegalAccessException e7) {
            e = e7;
            Logger.w(TAG, "catch exception when create sslSocketFactory", e);
        }
    }

    private RequestTask.Factory createFactory() {
        RequestTask.Factory createOkHttpFactory = createOkHttpFactory();
        if (createOkHttpFactory == null) {
            return new URLConnectionRequestTaskFactory(this);
        }
        return createOkHttpFactory;
    }

    private RequestTask.Factory createOkHttpFactory() {
        Throwable e;
        try {
            int parseInt;
            String[] split = fuv.ˊˋ().split("\\.");
            if (split.length > 0) {
                try {
                    parseInt = Integer.parseInt(split[split.length - 1]);
                } catch (Throwable e2) {
                    Logger.w(TAG, "maybe version error?", e2);
                }
                if (parseInt >= EMUI_INTERNAL_OKHTTP_VERSION_DATE) {
                    return new OkRequestTaskFactory(this);
                }
                return null;
            }
            parseInt = 0;
            if (parseInt >= EMUI_INTERNAL_OKHTTP_VERSION_DATE) {
                return new OkRequestTaskFactory(this);
            }
        } catch (NoClassDefFoundError e3) {
            e2 = e3;
            Logger.w(TAG, "is this type you want?", e2);
            return null;
        } catch (NoSuchMethodError e4) {
            e2 = e4;
            Logger.w(TAG, "is this type you want?", e2);
            return null;
        }
        return null;
    }

    private RequestTask.Factory createCronetFactory(Context context) {
        if (context == null || !CronetNegotiateManager.getInstance().isAvaliable()) {
            return null;
        }
        return CronetRequestTaskFactory.getInstance(context);
    }

    public RequestTask.Factory getFactory(Request request) {
        HttpUrl url = request.getUrl();
        if (url == null) {
            return this.defaultFactory;
        }
        String host = url.getHost();
        int port = url.getPort();
        if (this.quicEnabled && CronetNegotiateManager.getInstance().isEnableQuic(host, port).booleanValue()) {
            if (this.cronetFactory == null) {
                try {
                    this.cronetFactory = createCronetFactory(ContextUtil.getContext());
                } catch (Throwable th) {
                    Logger.e(TAG, "fail to get cronet factory, exception name:" + th.getClass().getSimpleName());
                    this.cronetFactory = null;
                }
            }
            if (this.cronetFactory != null) {
                return this.cronetFactory;
            }
        }
        return this.defaultFactory;
    }

    public boolean quicEnabled() {
        return this.quicEnabled;
    }

    RCEventListener.Factory getEventListenerFactory() {
        return this.eventListenerFactory;
    }

    public List<Interceptor> getInterceptors() {
        return this.interceptors;
    }

    public List<Interceptor> getNetworkInterceptors() {
        return this.networkInterceptors;
    }

    private synchronized void addInterceptor(Interceptor interceptor) {
        if (interceptor instanceof PluginInterceptor) {
            for (Interceptor equals : this.interceptors) {
                if (interceptor.equals(equals)) {
                    break;
                }
            }
            this.interceptors.add(interceptor);
        }
    }

    private synchronized void addNetworkInterceptor(Interceptor interceptor) {
        if (interceptor instanceof PluginInterceptor) {
            for (Interceptor equals : this.networkInterceptors) {
                if (interceptor.equals(equals)) {
                    break;
                }
            }
            this.networkInterceptors.add(interceptor);
        }
    }

    public int getRetryTimeOnConnectionFailure() {
        return this.clientConfiguration.getRetryTimeOnConnectionFailure();
    }

    public int getConnectTimeout() {
        return this.clientConfiguration.getConnectTimeout();
    }

    public int getReadTimeout() {
        return this.clientConfiguration.getReadTimeout();
    }

    public int getWriteTimeout() {
        return this.clientConfiguration.getWriteTimeout();
    }

    public int getPingInterval() {
        return this.clientConfiguration.getPingInterval();
    }

    @Deprecated
    public DNKeeper getDnKeeper() {
        return this.dnKeeper;
    }

    public ClientConfiguration getClientConfiguration() {
        return this.clientConfiguration;
    }

    public RCDns getDns() {
        return this.dns;
    }

    public X509TrustManager getTrustManager() {
        return this.trustManager;
    }

    public SSLSocketFactory getSslSocketFactory() {
        return this.sslSocketFactory;
    }

    public HostnameVerifier getHostnameVerifier() {
        return this.hostnameVerifier;
    }

    public HttpCache getCache() {
        return this.cache;
    }

    public Submit newSubmit(Request request) {
        if (this.trustManager == null || this.sslSocketFactory == null) {
            trustManagerAndFactory();
            this.defaultFactory = createFactory();
        }
        return new BuildInSubmit(this, fillClientConfig(request));
    }

    private Request fillClientConfig(Request request) {
        if (request.isClientConfigurationModified()) {
            return request;
        }
        return request.newBuilder().clientConfiguration(this.clientConfiguration).clientConfigurationModified(false).build();
    }

    public Builder newBuilder() {
        return new Builder().addInterceptors(this.interceptors).addNetInterceptors(this.networkInterceptors).clientConfiguration(this.clientConfiguration.newBuilder()).sslSocketFactory(this.sslSocketFactory, this.trustManager).hostnameVerifier(this.hostnameVerifier).eventListenerFactory(this.eventListenerFactory).isReportable(this.isReportable).enableQuic(this.quicEnabled);
    }

    public boolean isWeakNetworkRetryEnable() {
        return this.weakNetworkRetryEnable;
    }
}
